package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    // EmployeeLoginVO：响应给前端的数据
    // EmployeeLoginDTO：前端请求的数据
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        // 随便放入
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        // claims.put(JwtClaimsConstant.USERNAME,employee.getUsername());

         /*      放入密钥和生成时间，这里没有写死，而是像AliOSS一样通过配置类的方式
                 JwtProperties是一个配置类，通过@ConfigurationProperties(prefix = "sky.jwt")注解来指定配置前缀
                 在application.yml配置后会自动将值注入到JwtProperties对应的属性中
                 */

        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

/*        lombok中的@Builder来创建对象 User u = User.builder()
         .name()
         .age()
         .build();*/

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("登出")
    public Result<String> logout() {
        return Result.success();
    }

    //  /**+Enter：创建方法说明注释

    /**
     * 插入员工数据
     * @param employeeDTO
     * @return
     */
    @PostMapping
    @ApiOperation("插入员工信息")
    public Result insert(@RequestBody EmployeeDTO employeeDTO){
        log.info("请求的员工信息为：{}",employeeDTO);
        employeeService.insert(employeeDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询")
    // @RequestBody是接收请求体的，GET请求的参数是在请求行里
    // 不用加@RequestBody注解，springMVC会自动封装

    // TODO:操作时间显示格式不对
    // 解决：在WebMvcConfiguration中扩展Spring MVC的消息转换器，统一对日期类型进行格式化处理
    public Result<PageResult> list(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("员工分页查询请求数据为：{}",employeePageQueryDTO);
        PageResult pageResult = employeeService.list(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("status/{status}")
    @ApiOperation("启用，禁用员工")
    public Result prohibit(@PathVariable String status,String id){
        employeeService.prohibit(status,id);
        return Result.success();
    }

    @GetMapping("{id}")
    @ApiOperation("根据ID查询员工")
    public Result<Employee> getById(@PathVariable Long id){
        log.info("根据ID查询用户请求参数为:{}",id);
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }

    @PutMapping("/editPassword")
    @ApiOperation("修改密码")
    public Result editPassword(@RequestBody PasswordEditDTO passwordEditDTO){
        log.info("修改密码请求参数为：{}",passwordEditDTO);
        employeeService.editPassword(passwordEditDTO);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("编辑员工信息")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        employeeService.update(employeeDTO);
        return Result.success();
    }
}
